package org.microdream.easy.deploy.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import ice.server.comm.BaseServiceImpl;
import ice.server.comm.CommRepository;
import ice.server.comm.ParamBulider;
import ice.tool.PageVO;
import ice.tool.ResultMap;
import org.apache.commons.lang3.StringUtils;
import org.microdream.easy.deploy.dao.DRentRepository;
import org.microdream.easy.deploy.po.CachePo;
import org.microdream.easy.deploy.po.DRent;
import org.microdream.easy.deploy.utils.DateUtil;
import org.microdream.easy.deploy.utils.ListUtil;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DRentService extends BaseServiceImpl<DRent, Integer>{

    @Resource
    private DRentRepository dRentRepository;

    @Override
    public CommRepository<DRent, Integer> getRepository() {

        return dRentRepository;
    }

    @Override
    public Integer getIdenty(DRent t) {
        return t.getId();
    }

    /**
     * 分页查询客户信息
     * */
    public Map searchByPage(Map gson){
        Map resultMap = new HashMap();
        PageVO<DRent> pv = new PageVO<>();
        Page<DRent> p = this.findPage(gson);
        List<DRent> dRents = p.getContent();
        if(!CollectionUtils.isEmpty(dRents)){
//            Map<String,String> map = CachePo.dictionarySingleMap;
            dRents.forEach(dRent -> {
                dRent.set("showRentType",CachePo.getName("rent_type", "" + dRent.getRentType()));
                dRent.set("showRentScale",CachePo.getName("rent_scale", "" + dRent.getRentScale()));
                dRent.set("showRentTrade",CachePo.getName("rent_trade", "" + dRent.getRentTrade()));
                dRent.set("showUpdateTime", DateUtil.TimestampToString_1(dRent.getUpdateTime()));
            });
        }
        resultMap.put("total", p.getTotalElements());
        resultMap.put("rows", dRents);
        resultMap.put("page",p.getNumber());
        return resultMap;
    }

    /**
     * 新增客户信息
     * */
    public Map add(DRent dRent){
        Integer flag = this.save(dRent);
        return ResultMap.build().add(flag>0).get();
    }

    /**
     * 修改客户信息
     *
     * */
    public Map upd(DRent dRent){
        Integer flag = this.save(dRent);
        return ResultMap.build().update(flag>0).get();
    }

    /**
     * 根据ids单条或者批量删除客户信息，假删除
     * */
    public Map del(List<Integer> ids){
        List<DRent> dRents = this.find(ParamBulider.build().add("id_in",ListUtil.ListToString(ids,",")).get());
        if(CollectionUtils.isEmpty(dRents)){
            return ResultMap.build().warningState().message("无效数据").get();
        }
        dRents.forEach(dRent -> {
            dRent.setIsDelete(1);
        });
        return ResultMap.build().delete(this.save(dRents)).get();

    }

    /**
     * 根据客户id锁定或者解锁机构
     * @param id 主键id
     * */
    public Map lock(Integer id){
        DRent dRent = this.get(id);
        dRent.setIslock(dRent.getIslock()==1?0:1);
        this.save(dRent);
        return ResultMap.build().successState().successCode().message(dRent.getIslock()==0?"解锁成功":"锁定成功").get();
    }

    /**
     * 得到所有可用客户的key-value键值对
     * */
    public JSONArray getRentKeyValue(String keyName,String valueName){
        JSONArray jsonArray = new JSONArray();
        List<DRent> dRents = this.find(ParamBulider.build().addEqual("isDelete",0).get());
        dRents.forEach(dRent -> {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put(StringUtils.isNotEmpty(keyName)?keyName:"key",dRent.getRentCode());
            jsonObject.put(StringUtils.isNotEmpty(valueName)?valueName:"value",dRent.getRentName());
            jsonArray.add(jsonObject);
        });
        return jsonArray;
    }
}
